#include "OCMain/FileUtil.h"
#include "OCMain/Environment.h"
#include "OCMain/Log.h"
#include "OCServer/ServerUtil.h"
#include "ProjectTimer.h"

using namespace OCPP;

ProjectTimer* gProjectTimer = NULL;

template <typename T>
inline std::vector<T> getYmlVec(const YAML::Node& node)
{
	std::vector<T> vec;
	if (node.Type() == YAML::NodeType::Sequence)
	{
		try {
			for (const auto& c : node)
			{
				vec.push_back(c.as<T>());
			}
		}
		catch (...) {}
	}

	return vec;
}

void init()
{
	String yamlFileName = Singleton(Environment).getAppPath() + "config/OCPP_ControllerMicroWin3dService.yml";
	try {
		YAML::Node node = YAML::LoadFile(yamlFileName);
		gProjectTimer = new ProjectTimer();

		for (const auto& child : node)
		{
			String name = child.first.as<String>();
#if WIN32
			if (name == "win32")
#else
			if (name == "linux")
#endif
			{
				gProjectTimer->_dataPath = getYmlValue<String>(child.second, "dataPath", "");
				gProjectTimer->_outputPath = getYmlValue<String>(child.second, "outputPath", "");
				gProjectTimer->_expiredTime = getYmlValue<int>(child.second, "expiredTime", 10);
				
				LOG_INFO << "dataPath:" << gProjectTimer->_dataPath << "\n";
				LOG_INFO << "outputPath:" << gProjectTimer->_outputPath << "\n";
				break;
			}
		}

		gProjectTimer->start();
	}
	catch (const YAML::Exception& e) {
		LOG_ERROR << e.msg << "\n";
	}
}

void release()
{
	CORE_SAFE_DELETE(gProjectTimer);
}